package com.ruoyi.web.controller.module.asset;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.asset.EstateAssetBorrowRecord;
import com.ruoyi.system.domain.module.asset.EstateAssetCheckRecord;
import com.ruoyi.system.service.module.asset.IEstateAssetBorrowRecordService;
import com.ruoyi.system.service.module.asset.IEstateAssetService;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import com.ruoyi.system.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 资产借用记录Controller
 *
 * @author ruoyi
 * @date 2025-01-24
 */
@Validated
@Api(value = "资产借用记录控制器", tags = {"资产借用记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/borrowRecord")
public class EstateAssetBorrowRecordController extends BaseController {

    private final IEstateAssetBorrowRecordService estateAssetBorrowRecordService;
	private final IEstateService estateService;
	private final IWxUserService wxUserService;
	private final ISysUserService sysUserService;
	private final IEstateAssetService estateAssetService;
    /**
     * 查询资产借用记录列表
     */
    @ApiOperation("查询资产借用记录列表")
    @PreAuthorize("@ss.hasPermi('module:borrowRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateAssetBorrowRecord> list(@Validated EstateAssetBorrowRecord estateAssetBorrowRecord) {
        QueryWrapper<EstateAssetBorrowRecord> qw = new QueryWrapper<>();
		if(estateAssetBorrowRecord.getAssetId()!=null){
			qw.eq("asset_id",estateAssetBorrowRecord.getAssetId());
		}
		if(estateAssetBorrowRecord.getState()!=null){
			qw.eq("state",estateAssetBorrowRecord.getState());
		}
		qw.orderByDesc("reback_time");
        TableDataInfo<EstateAssetBorrowRecord> td = PageUtils.buildDataInfo(estateAssetBorrowRecordService.page(PageUtils.buildPage(),qw));
        List<EstateAssetBorrowRecord> list = td.getRows();
		if (list.size()>0){
			for (EstateAssetBorrowRecord list1:list){
				if (list1.getEstateId()!=null){
					list1.setEstateName(estateService.findById(list1.getEstateId()).getEstateName());
				}
				if (list1.getAssetId()!=null){
					list1.setAssetName(estateAssetService.findById(list1.getAssetId()).getAssetName());
				}
				if (list1.getFromRecordId()!=null){
					EstateAssetBorrowRecord estateAssetBorrow = estateAssetBorrowRecordService.findById(estateAssetBorrowRecord.getFromRecordId());
					if (estateAssetBorrow!=null && estateAssetBorrow.getRealName()!=null){
						list1.setFromRecordName(estateAssetBorrow.getRealName());
					}
				}else {
					list1.setFromRecordName("无");
				}
			}
		}
        return td;
    }

    /**
     * 导出资产借用记录列表
     */
    @ApiOperation("导出资产借用记录列表")
    @PreAuthorize("@ss.hasPermi('module:borrowRecord:export')")
    @Log(title = "资产借用记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateAssetBorrowRecord> export(@Validated EstateAssetBorrowRecord estateAssetBorrowRecord) {
        List<EstateAssetBorrowRecord> list = estateAssetBorrowRecordService.list();
        ExcelUtil<EstateAssetBorrowRecord> util = new ExcelUtil<EstateAssetBorrowRecord>(EstateAssetBorrowRecord.class);
        return util.exportExcel(list, "资产借用记录");
    }

    /**
     * 获取资产借用记录详细信息
     */
    @ApiOperation("获取资产借用记录详细信息")
    @PreAuthorize("@ss.hasPermi('module:borrowRecord:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateAssetBorrowRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateAssetBorrowRecord estateAssetBorrowRecord = estateAssetBorrowRecordService.getById(id);
        return AjaxResult.success(estateAssetBorrowRecord);
    }

    /**
     * 新增资产借用记录
     */
    @ApiOperation("新增资产借用记录")
    @PreAuthorize("@ss.hasPermi('module:borrowRecord:add')")
    @Log(title = "资产借用记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateAssetBorrowRecord estateAssetBorrowRecord) {
        return toAjax(estateAssetBorrowRecordService.save(estateAssetBorrowRecord) ? 1 : 0);
    }

    /**
     * 修改资产借用记录
     */
    @ApiOperation("修改资产借用记录")
    @PreAuthorize("@ss.hasPermi('module:borrowRecord:edit')")
    @Log(title = "资产借用记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateAssetBorrowRecord estateAssetBorrowRecord) {
        return toAjax(estateAssetBorrowRecordService.updateById(estateAssetBorrowRecord) ? 1 : 0);
    }

    /**
     * 删除资产借用记录
     */
    @ApiOperation("删除资产借用记录")
    @PreAuthorize("@ss.hasPermi('module:borrowRecord:remove')")
    @Log(title = "资产借用记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateAssetBorrowRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
